package org.example.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import lombok.Data;

/**
 * 支付宝配置类
 */
@Data
public class AlipayConfig {

    /**
     * 应用ID - 从支付宝开放平台获取
     */
    private String appId;

    /**
     * 应用私钥 - RSA2格式
     */
    private String privateKey;

    /**
     * 支付宝公钥 - RSA2格式
     */
    private String alipayPublicKey;

    /**
     * 服务器地址
     * 正式环境: https://openapi.alipay.com/gateway.do
     * 沙箱环境: https://openapi-sandbox.dl.alipaydev.com/gateway.do
     */
    private String serverUrl = "https://openapi.alipay.com/gateway.do";

    /**
     * 请求格式，默认JSON
     */
    private String format = "json";

    /**
     * 字符集，默认UTF-8
     */
    private String charset = "UTF-8";

    /**
     * 签名类型，推荐RSA2
     */
    private String signType = "RSA2";

    /**
     * 是否使用沙箱环境
     */
    private boolean sandbox = false;

    /**
     * 构造方法
     */
    public AlipayConfig(String appId, String privateKey, String alipayPublicKey) {
        this.appId = appId;
        this.privateKey = privateKey;
        this.alipayPublicKey = alipayPublicKey;
    }

    /**
     * 构造方法 - 可指定是否使用沙箱
     */
    public AlipayConfig(String appId, String privateKey, String alipayPublicKey, boolean sandbox) {
        this.appId = appId;
        this.privateKey = privateKey;
        this.alipayPublicKey = alipayPublicKey;
        this.sandbox = sandbox;
        if (sandbox) {
            this.serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
        }
    }

    /**
     * 创建AlipayClient实例
     * @return AlipayClient
     */
    public AlipayClient createAlipayClient() {
        return new DefaultAlipayClient(
            serverUrl,
            appId,
            privateKey,
            format,
            charset,
            alipayPublicKey,
            signType
        );
    }
}
